var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    navList: [],
    categoryList: [],
    currentCategory: {},
    currentCat: {},
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0,
    childlist: [],
    catData: [],
    goodsData: [],
    fontname: '',
    name: '全部'
  },
  onLoad: function(options) {
    this.getCatalog();
  },
  getCatalog: function() {
    //CatalogList
    let that = this;
    util.request(api.getGoodsCat, "GET").then(function(res) {
      let catres = [];
      catres.push({
        'catId': '全部',
        'catName': '全部'
      });
      for (let i = 0; i < res.length; i++) {
        catres.push(res[i]);
      }
      that.setData({
        navList: catres,
        catData: catres,
      })
    })
    wx.showLoading({
      title: '加载中...',
    });
    that.setData({
      currentCategory: {
        id: '全部',
        wap_banner_url: '/static/images/bg.png',
        front_name: '全部',
        name: '全部'
      }
    })
    wx.hideLoading();
    let body = {
      cat1: that.data.currentCategory.id,
      cat2: that.data.currentCat.id
    }
    util.request(api.getGoodsByCat, body)
      .then(function(res) {
        that.setData({
          goodsData: res,
          currentCategory: {
            id: '全部',
            wap_banner_url: '/static/images/bg.png',
            front_name: '全部',
            name: '全部',
            subCategoryList: res
          }
        })

      });


  },
  getCurrentCategory: function(event) {
    let that = this;
    let body = {
      cat1: that.data.currentCategory.id,
      cat2: that.data.currentCat.id
    }
    util.request(api.getGoodsByCat, body)
      .then(function(res) {
        that.setData({
          goodsData: res,
          currentCategory: {
            id: that.data.currentCategory.id,
            wap_banner_url: '/static/images/bg.png',
            front_name: that.data.fontname,
            name: event.currentTarget.dataset.name,
            subCategoryList: res
          }
        })
      });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  getList: function() {
    var that = this;
    // util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.cat_id)
    //   .then(function (res) {
    //     that.setData({
    //       categoryList: res.data,
    //     });
    //   });
  },
  switchCate: function(event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (event.currentTarget.dataset.id == '全部') {
      that.setData({
        childlist: []
      })
      let body = {
        cat1: event.currentTarget.dataset.id,
        cat2: null
      }
      util.request(api.getGoodsByCat, body)
        .then(function(res) {
          that.setData({
            goodsData: res,
            fontname: event.currentTarget.dataset.name,
            currentCategory: {
              id: event.currentTarget.dataset.id,
              wap_banner_url: '/static/images/bg.png',
              front_name: event.currentTarget.dataset.name,
              name: '全部',
              subCategoryList: res
            }
          })
        });
    } else {
      let catsData = [];
      catsData.push({
        'catId': '全部',
        'catName': '全部'
      });
      for (let i = 0; i < this.data.catData.length; i++) {
        if (this.data.catData[i].catId == event.currentTarget.dataset.id) {
          if (this.data.catData[i].child != null) {
            for (let j = 0; j < this.data.catData[i].child.length; j++) {
              catsData.push(this.data.catData[i].child[j]);
            }
            that.setData({
              childlist: catsData
            })
          }else{
            that.setData({
              childlist: catsData
            })
          }

        }
      }
    }
    that.setData({
      thiscat: event.currentTarget.dataset.id
    })

    this.setData({
      fontname: event.currentTarget.dataset.name,
      currentCategory: {
        id: event.currentTarget.dataset.id,
        wap_banner_url: '/static/images/bg.png',
        front_name: event.currentTarget.dataset.name,
        name: that.data.name,
        subCategoryList: that.data.goodsData
      }
    })
  },
  childCat: function(event) {
    let that = this;
    if (that.data.currentCat.id == event.currentTarget.dataset.id) {
      that.setData({
        childlist: []
      })
    }
    that.setData({
      name: event.currentTarget.dataset.name,
      currentCat: {
        id: event.currentTarget.dataset.id
      },
      childlist: []
    })
    this.getCurrentCategory(event);
  }
})